import {
  Controller,
  Post,
  Body,
  Get,
  Query,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { PaymentMethodsService } from './payment-methods.service';
import { StripeService } from './stripe.service';

@Controller('payment-methods')
export class PaymentMethodsController {
  constructor(
    private readonly paymentMethodsService: PaymentMethodsService,
    private readonly stripeService: StripeService,
  ) {}

  @Get('by-country')
  getPaymentMethodsByCountry(@Query('countryCode') countryCode: string) {
    const methods = this.paymentMethodsService.getPaymentMethodsByCountry(countryCode);
    return {
      success: true,
      data: methods
    };
  }

  @Get()
  getAllRegionalPaymentMethods(@Query('country') country?: string) {
    if (country) {
      // 如果提供了国家参数，则返回该国家的支付方式
      const methods = this.paymentMethodsService.getPaymentMethodsByCountry(country);
      return {
        success: true,
        data: methods
      };
    } else {
      // 否则返回所有国家的支付方式映射
      const methods = this.paymentMethodsService.getAllRegionalPaymentMethods();
      return {
        success: true,
        data: methods
      };
    }
  }

  @Get('all')
  getAllRegionalPaymentMethodsLegacy() {
    const methods = this.paymentMethodsService.getAllRegionalPaymentMethods();
    return {
      success: true,
      data: methods
    };
  }

  @Post('process')
  async processPayment(
    @Body('method') method: string,
    @Body('amount') amount: number,
    @Body('description') description: string,
    @Body('paymentData') paymentData?: any,
  ) {
    try {
      let result;
      
      switch (method) {
        case 'wechat-pay':
          result = await this.paymentMethodsService.processWeChatPay(amount, description);
          break;
          
        case 'alipay':
          result = await this.paymentMethodsService.processAlipay(amount, description);
          break;
          
        case 'apple-pay':
          // 对于Apple Pay，我们可能需要额外的数据
          result = await this.paymentMethodsService.processApplePay(paymentData || {}, amount);
          break;
          
        case 'yoomoney':
          result = await this.paymentMethodsService.processYooMoney(amount, description);
          break;
          
        case 'mir':
          result = await this.paymentMethodsService.processMirPay(amount, description);
          break;
          
        case 'stripe-card':
          // 对于Stripe卡支付，我们创建一个支付意图
          const paymentIntent = await this.stripeService.createPaymentIntent(
            amount,
            'usd', // 默认货币，可以根据需要调整
            description,
          );
          result = {
            client_secret: paymentIntent.client_secret,
            id: paymentIntent.id,
          };
          break;
          
        default:
          throw new Error(`Unsupported payment method: ${method}`);
      }
      
      return {
        success: true,
        data: result
      };
    } catch (error) {
      throw new HttpException(
        {
          status: HttpStatus.INTERNAL_SERVER_ERROR,
          error:
            error instanceof Error
              ? error.message
              : 'An unknown error occurred',
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Post('stripe/create-payment-intent')
  async createStripePaymentIntent(
    @Body('amount') amount: number,
    @Body('currency') currency: string,
    @Body('description') description: string,
  ) {
    try {
      const paymentIntent = await this.stripeService.createPaymentIntent(
        amount,
        currency,
        description,
      );
      return {
        success: true,
        data: {
          client_secret: paymentIntent.client_secret,
          id: paymentIntent.id,
        }
      };
    } catch (error) {
      throw new HttpException(
        {
          status: HttpStatus.INTERNAL_SERVER_ERROR,
          error:
            error instanceof Error
              ? error.message
              : 'An unknown error occurred',
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}